package com.yjdb.sanyuantang.modules.Common.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yjdb.sanyuantang.aop.OperationLogAnnotation;
import com.yjdb.sanyuantang.common.result.CommonResult;
import com.yjdb.sanyuantang.common.result.ResultCode;
import com.yjdb.sanyuantang.modules.Common.DTO.common.CommonMenuDTO;
import com.yjdb.sanyuantang.modules.Common.service.CommonLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * packageName com.yjdb.sanyuantang.modules.common
 *
 * @author 混沌酱
 * @version 1.0
 * @className LoginController
 * @date 2024/3/15
 * @description TODO
 */
@Tag(name = "登录模块")
@RequestMapping("/common/login")
@RestController
public class CommonLoginController {

    private static final Logger logger = LoggerFactory.getLogger(CommonLoginController.class);
    @Resource
    private CommonLoginService commonLoginService;


    // 后台登录(账号密码)
    @OperationLogAnnotation(operModule = "登录模块", operFunction = "后台密码登录")
    @Operation(summary = "后台密码登录")
    @RequestMapping(path = "/admin_password", method = RequestMethod.GET)
    @SaIgnore
    public CommonResult<String> admin_password(
            @Parameter(name = "username",description = "用户名",required = true)  String username,
            @Parameter(name = "password",description = "密码",required = true)  String password
    ){
        try {
            String token = commonLoginService.admin_password(username,password);
            return CommonResult.success(token,"登录成功");
        }catch (Exception e){
            logger.debug("登录失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }


    // 获取菜单
    @Operation(summary = "获取菜单")
    @RequestMapping(path = "/get_menu", method = RequestMethod.GET)
    public CommonResult<List<CommonMenuDTO>> get_menu(){
        try {
            List<CommonMenuDTO> commonMenuDTOS = commonLoginService.get_menu();
            return CommonResult.success(commonMenuDTOS);
        }catch (Exception e){
            logger.debug("获取失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }

    // 获取子集菜单/按钮
    @Operation(summary = "获取子集菜单/按钮")
    @RequestMapping(path = "/get_menu_son", method = RequestMethod.GET)
    public CommonResult<List<CommonMenuDTO>> get_menu_son(
            @Parameter(name = "id",description = "父级id",required = true)  Integer id
    ){
        try {
            List<CommonMenuDTO> commonMenuDTOS = commonLoginService.get_menu_son(id);
            return CommonResult.success(commonMenuDTOS);
        }catch (Exception e){
            logger.debug("获取失败", e);
            return CommonResult.failed(e.getMessage());
        }
    }
}
